//
//  YWDownloadArchiverTool.m
//  videoTest3
//
//  Created by jun peng on 2022/7/28.
//

#import "YWDownloadDataBaseTool.h"

@implementation YWDownloadDataBaseTool

static FMDatabaseQueue *_queue;

+ (void)initialize
{
    // 0.获得沙盒中的数据库文件名
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"gbb.sqlite"];
    
    // 1.创建队列
    _queue = [FMDatabaseQueue databaseQueueWithPath:path];
    
    // 2.创表
    [_queue inDatabase:^(FMDatabase *db) {
        [db executeUpdate:@"create table if not exists t_status (id integer primary key autoincrement, status blob, VideoUrl text);"];
    }];
    
    [_queue inDatabase:^(FMDatabase *db) {
        [db executeUpdate:@"create table if not exists t_finish_status (id integer primary key autoincrement, status blob, VideoUrl text);"];
    }];
    
    
}


// -----------正在任务中的（包括了所有除了完成的其它枚举）


//增
+ (void)addDownloadingStatus:(YWDownloadStatus3 *)status{
    
     [_queue inDatabase:^(FMDatabase *db) {
         // 1.获得需要存储的数据
         NSData *data = [NSKeyedArchiver archivedDataWithRootObject:status];
         
         // 2.存储数据
         [db executeUpdate:@"insert into t_status (status,VideoUrl) values(?,?)", data,status.VideoUrl];
     }];
}
+ (void)addDownloadingStatuses:(NSArray *)statusArray{
    
    for (YWDownloadStatus3 *status in statusArray) {
        [self addDownloadingStatus:status];
    }
}

//删
+ (void)removeDownloadingStatus:(YWDownloadStatus3 *)status{
    [_queue inDatabase:^(FMDatabase *db) {
    
        [db executeUpdate:@"delete from t_status where VideoUrl = ?", status.VideoUrl];
    }];
}

+ (void)removeDownloadingAllNewsDataStatus{
    [_queue inDatabase:^(FMDatabase *db) {
    
        [db executeUpdate:@"delete from t_status"];
    }];
}

//改
+ (void)updateDownloadingStatus:(YWDownloadStatus3 *)status{
    
    [_queue inDatabase:^(FMDatabase *db) {
      
        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:status];
        
        [db executeUpdate:@"update t_status set status = ? where VideoUrl = ?",data,status.VideoUrl];
    }];
}

//查
+ (NSArray *)getDownloadingAllNewsDataStatus{
    // 1.定义数组
    __block NSMutableArray *statusArray = nil;
    
    // 2.使用数据库
    [_queue inDatabase:^(FMDatabase *db) {
        // 创建数组
        statusArray = [NSMutableArray array];
        
        FMResultSet *rs = [db executeQuery:@"select * from t_status;"];
      
        
        while (rs.next) {
            NSData *data = [rs dataForColumn:@"status"];
            YWDownloadStatus3 *status = [NSKeyedUnarchiver unarchiveObjectWithData:data];
            [statusArray addObject:status];
        }
    }];
    
    // 3.返回数据
    return statusArray;
}


+ (YWDownloadStatus3 *)getDownloadingStatusWithVideoUrl:(NSString *)VideoUrl{
    // 1.定义数组
    __block NSMutableArray *statusArray = nil;
    
    // 2.使用数据库
    [_queue inDatabase:^(FMDatabase *db) {
        // 创建数组
        statusArray = [NSMutableArray array];
        
        FMResultSet *rs = [db executeQuery:@"select * from t_status where VideoUrl = ?;",VideoUrl];
      
        
        while (rs.next) {
            NSData *data = [rs dataForColumn:@"status"];
            YWDownloadStatus3 *status = [NSKeyedUnarchiver unarchiveObjectWithData:data];
            [statusArray addObject:status];
        }
    }];
    
    if (statusArray.count > 0) {
        
        // 3.返回数据
        return statusArray[0];
    }else{
        
        return nil;
    }
    
}




// -----------完成的

//t_finish_status

//增
+ (void)addFinishStatus:(YWDownloadStatus3 *)status{
    [_queue inDatabase:^(FMDatabase *db) {
        // 1.获得需要存储的数据
        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:status];
        
        // 2.存储数据
        [db executeUpdate:@"insert into t_finish_status (status,VideoUrl) values(?,?)", data,status.VideoUrl];
    }];
    
}

+ (void)addFinishStatuses:(NSArray *)statusArray{
    for (YWDownloadStatus3 *status in statusArray) {
        [self addFinishStatus:status];
    }
}
//删
+ (void)removeFinishStatus:(YWDownloadStatus3 *)status{
    [_queue inDatabase:^(FMDatabase *db) {
    
        [db executeUpdate:@"delete from t_finish_status where VideoUrl = ?", status.VideoUrl];
    }];
}

+ (void)removeFinishAllNewsDataStatus{
    [_queue inDatabase:^(FMDatabase *db) {
    
        [db executeUpdate:@"delete from t_finish_status"];
    }];
}

//改
+ (void)updateFinishStatus:(YWDownloadStatus3 *)status{
    [_queue inDatabase:^(FMDatabase *db) {
      
        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:status];
        
        [db executeUpdate:@"update t_finish_status set status = ? where VideoUrl = ?",data,status.VideoUrl];
    }];
}

//查
+ (NSArray *)getFinishAllNewsDataStatus{
    // 1.定义数组
    __block NSMutableArray *statusArray = nil;
    
    // 2.使用数据库
    [_queue inDatabase:^(FMDatabase *db) {
        // 创建数组
        statusArray = [NSMutableArray array];
        
        FMResultSet *rs = [db executeQuery:@"select * from t_finish_status;"];
      
        
        while (rs.next) {
            NSData *data = [rs dataForColumn:@"status"];
            YWDownloadStatus3 *status = [NSKeyedUnarchiver unarchiveObjectWithData:data];
            [statusArray addObject:status];
        }
    }];
    
    // 3.返回数据
    return statusArray;
}

+ (YWDownloadStatus3 *)getFinishStatusWithVideoUrl:(NSString *)VideoUrl{
    // 1.定义数组
    __block NSMutableArray *statusArray = nil;
    
    // 2.使用数据库
    [_queue inDatabase:^(FMDatabase *db) {
        // 创建数组
        statusArray = [NSMutableArray array];
        
        FMResultSet *rs = [db executeQuery:@"select * from t_finish_status where VideoUrl = ?;",VideoUrl];
      
        
        while (rs.next) {
            NSData *data = [rs dataForColumn:@"status"];
            YWDownloadStatus3 *status = [NSKeyedUnarchiver unarchiveObjectWithData:data];
            [statusArray addObject:status];
        }
    }];
    
    if (statusArray.count > 0) {
        
        // 3.返回数据
        return statusArray[0];
    }else{
        
        return nil;
    }
}

@end
